package com.mall.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.pageResult.PageResult;
import com.mall.pojo.PmsProductAttribute;
import com.mall.pojo.PmsProductCategoryAttributeRelation;
import com.mall.service.PmsProductAttributeService;
import com.mall.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/productAttribute")
public class PmsProductAttributeController {

    @Autowired
    private PmsProductAttributeService pmsProductAttributeService;

    //根据商品分类的id获取商品属性及属性分类
    @GetMapping("/attrInfo/{productCategoryId}")
    public Result selectById(@PathVariable("productCategoryId") Integer productCategoryId) {
        PmsProductCategoryAttributeRelation ppcar = pmsProductAttributeService.selectById(productCategoryId);
        return Result.success(ppcar, "操作成功");
    }

    //添加商品属性信息
    @PostMapping("create")
    public Result create(@RequestBody PmsProductAttribute pmsProductAttribute) {
        pmsProductAttributeService.cteate(pmsProductAttribute);
        return Result.success(null, "操作成功");
    }

    //批量删除商品属性
    @PostMapping("/delete")
    public Result delete(@RequestParam("ids") List<Integer> ids) {
        pmsProductAttributeService.delete(ids);
        return Result.success(null, "操作成功");
    }

    //根据分类查询属性列表或参数列表
    @GetMapping("/list/{cid}")
    public Result<PageResult<PmsProductAttribute>> list(
            @PathVariable("cid") Integer cid,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam("type") Integer type) {
        PageResult<PmsProductAttribute> pageResult = new PageResult<>();
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);
        Page<PmsProductAttribute> page = pmsProductAttributeService.list(cid, pageNum, pageSize, type);

        if (CollUtil.isEmpty(page.getRecords())) {
            pageResult.setList(new ArrayList<>());
            return Result.success(pageResult, "操作成功");
        }
        pageResult.setList(page.getRecords());
        pageResult.setTotal(Convert.toInt(page.getTotal()));
        pageResult.setTotalPage(Convert.toInt(page.getPages()));
        return Result.success(pageResult, "操作成功");
    }

    //修改商品属性信息
    @PostMapping("/update/{id}")
    public Result update(@PathVariable("id") Integer id, @RequestBody PmsProductAttribute pmsProductAttribute) {
        pmsProductAttributeService.update(id, pmsProductAttribute);
        return Result.success(null, "操作成功");
    }

    //查询单个商品属性
    @GetMapping("/{id}")
    public Result<PmsProductAttribute> select(@PathVariable("id") Integer id) {
        PmsProductAttribute pmsProductAttribute = pmsProductAttributeService.select(id);
        return Result.success(pmsProductAttribute,"操作成功");
    }

}
